package com.ts.action;

import com.ts.config.Configuration;
import com.ts.dto.FileWrapper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.contexts.Contexts;

@Name("fileuploadaction")
public class FileUploadAction implements Serializable {

    @In
    ResourceBundle resourceBundle;
    private FileWrapper filewrapper;

    @Create
    public void init(){
        filewrapper=new FileWrapper();
    }

    public void upload() {
        saveToDisk(resourceBundle.getString(Configuration.IMAGES_SAVE_LOC_HANDLE_NAME));
        List<String> images=(List<String>) Contexts.getApplicationContext().get(Configuration.IMAGES_HANDLE_NAME);
        if(images==null){
           images=new ArrayList<String>();
        }

        images.add(0, filewrapper.getFileName());
        Contexts.getApplicationContext().set(Configuration.IMAGES_HANDLE_NAME, images.size() <= Configuration.IMAGE_DISPLAY_COUNT ? images:images.subList(0, Configuration.IMAGE_DISPLAY_COUNT));

    }

    private void saveToDisk(String saveLocation) {
        filewrapper.saveToDisk(saveLocation);
    }

    public FileWrapper getFilewrapper() {
        return filewrapper;
    }

    public void setFilewrapper(FileWrapper filewrapper) {
        this.filewrapper = filewrapper;
    }
}
